package com.woyou.service;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.woyou.bean.MyAddress;
import com.woyou.service.location.BaiduLocationService;
import com.woyou.service.location.BaiduLocationService_;

/**
 * 
 * @author longtao.li
 *
 */
public class LocationLoopService {
	
	/**
	 * 定位超过一定距离的监听接口
	 * @author longtao.li
	 *
	 */
	public interface LocationChangeListener{
		void locationChange(BDLocation location);
	}
	
	private LocationChangeListener mLocationChangeListener;
	
	public void setLocationChangeListener(LocationChangeListener l){
		this.mLocationChangeListener = l;
	}
	
	private int  mLocationUpdateMinTime = 60000;  //定位请求间隔
	private static final int  DISTANCE = 200;                  //超出需要提示的距离
	
	private static LocationLoopService instance;
	private static final int MSG_LOCATION_LOOP = 0x00007;
	private BaiduLocationService mBaiduLocationService;
	
	private MyLocationListenner mMyLocationListenner;
	
	private LocationLoopService(Context context) {
		mBaiduLocationService = BaiduLocationService_.getInstance_(context);
		mMyLocationListenner = new MyLocationListenner();
		
	}

	public static LocationLoopService getInstance(Context context){
		if( instance == null ){
			instance = new LocationLoopService(context);
		}
		return instance;
	}
	
	private Handler mHandler = new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_LOCATION_LOOP:
				mBaiduLocationService.registerLocationListenner(mMyLocationListenner);
				mBaiduLocationService.startLocation();
				mHandler.sendEmptyMessageDelayed(MSG_LOCATION_LOOP, mLocationUpdateMinTime);
				break;

			default:
				break;
			}
		};
	};
	
	//百度地图2地之间的距离计算公式
	public  double getDistanceFromXtoY(double lat_a, double lng_a, double lat_b, double lng_b){
		double pk = (double) (180 / 3.14169);
	
		double a1 = lat_a / pk;
		double a2 = lng_a / pk;
		double b1 = lat_b / pk;
		double b2 = lng_b / pk;
	
		double t1 = Math.cos(a1) * Math.cos(a2) * Math.cos(b1) * Math.cos(b2);
		double t2 = Math.cos(a1) * Math.sin(a2) * Math.cos(b1) * Math.sin(b2);
		double t3 = Math.sin(a1) * Math.sin(b1);
		double tt = Math.acos(t1 + t2 + t3);
	
		return 6366000 * tt * 1.2; //乘1.2,无奈的修正，因计算出来的是直接距离，而软件需要步行距离
		//部分手机因内存问题，在通过百度地图计算距离时有可能出异常影响下单，故用此法变通下
	 }
	
	private BDLocation mLastLocation;
	/**
	 * 实现实位回调监听
	 */
	public class MyLocationListenner implements BDLocationListener {
		
		@Override
		public void onReceiveLocation(BDLocation location) {
			mBaiduLocationService.stop();
			mBaiduLocationService.unRegisterLocationListener(mMyLocationListenner);
			if( mLastLocation != null ){
				double latitude = mLastLocation.getLatitude();
				double longitude = mLastLocation.getLongitude();
				double latitude2 = location.getLatitude();
				double longitude2 = location.getLongitude();
				double distance = getDistanceFromXtoY(latitude, longitude, latitude2, longitude2);
				if( distance > DISTANCE ){
					mLastLocation = location;
					if( mLocationChangeListener!=null )
					    mLocationChangeListener.locationChange(mLastLocation);
				}
			}else{
				mLastLocation = location;
			}
			
		}
		
	}
	
	/**
	 * 开始运转
	 */
	public void start(){
		mHandler.sendEmptyMessage(MSG_LOCATION_LOOP);
	}
	
	/**
	 * 直接定位
	 */
	public void startLocation(){
		mBaiduLocationService.registerLocationListenner(mMyLocationListenner);
		mBaiduLocationService.startLocation();
	}
	
	/**
	 * 停止运转
	 */
	public void stop(){
		mBaiduLocationService.stop();
		mBaiduLocationService.unRegisterLocationListener(mMyLocationListenner);
		mHandler.removeMessages(MSG_LOCATION_LOOP);
	}
	
	/**
	 * 设置多久时间定位一次
	 * @param mils
	 */
	public void setLocationUpdateMinTime(int mils){
		this.mLocationUpdateMinTime = mils;
	}
	
	private MyAddress chosenAddr;  	//从地址定位界面传过来的经纬度

	public MyAddress getChosenAddr() {
		return chosenAddr;
	}

	public void setChosenAddr(MyAddress chosenAddr) {
		this.chosenAddr = chosenAddr;
	}
	
	public void clear(){
		chosenAddr = null;
	}

}

